using System;
using System.Linq;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.DirectoryServices;
using System.Collections.Generic;

namespace Hvam.SharePoint.Features.IIS {
    public class State : GlobalAdminPageBase {
        protected Literal literal;
        protected SPGridView grid;
        private IISData data;

        private void PopulateDataset() {
            data = new IISData();

            var states = IISManagementService.Local.States;

            foreach (IISState state in states.OrderBy(w => w.WebApplication)) {
                try {
                    SPWebApplication webApplication = SPWebService.ContentService.WebApplications[state.WebApplication];
                    SPUrlZone zone = (SPUrlZone)Enum.Parse(typeof(SPUrlZone), state.Zone.ToString());
                    var row = data.IISState.NewIISStateRow();
                    row.Server = state.Server;
                    row.WebApplication = webApplication.Name;
                    row.Url = webApplication.AlternateUrls.GetResponseUrl(zone).IncomingUrl;
                    row.Zone = zone.ToString();
                    row.WebSiteState = Enum.Parse(typeof(SPIisWebSite.SPIisServerState), state.WebSiteState.ToString()).ToString();
                    row.ApplicationPoolState = Enum.Parse(typeof(ApplicationPoolState), state.ApplicationPoolState.ToString()).ToString();
                    row.Timestamp = state.Timestamp.ToString();

                    data.IISState.AddIISStateRow(row);
                }
                catch { /* For now, just ignore */ }
            }
        }

        protected override void OnPreInit(EventArgs e) {
            base.OnPreInit(e);

            SPWebService service = SPFarm.Local.Services.GetValue<SPWebService>(string.Empty);
            if (service.Features[new Guid("e3a9c2f9-0065-4ff9-8809-3cffef10ef66")] == null) {
                throw new Exception("Feature not activated!");
            }
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            
            grid.AllowGrouping = true;
            grid.AllowGroupCollapse = true;
            grid.GroupField = "WebApplication";
            grid.GroupFieldDisplayName = "Web Application";
            grid.DisplayGroupFieldName = false;

            BoundField zoneField = new BoundField() { DataField = "Zone", HeaderText = "Zone" };
            grid.Columns.Add(zoneField);

            BoundField urlField = new BoundField() { DataField = "Url", HeaderText = "Url", HtmlEncode = false };
            grid.Columns.Add(urlField);

            BoundField serverField = new BoundField() { DataField = "Server", HeaderText = "Server" };
            grid.Columns.Add(serverField);

            BoundField webSiteField = new BoundField() { DataField = "WebSiteState", HeaderText = "Web Site" };
            grid.Columns.Add(webSiteField);

            BoundField applicationPoolField = new BoundField() { DataField = "ApplicationPoolState", HeaderText = "Application Pool" };
            grid.Columns.Add(applicationPoolField);

            BoundField timestampField = new BoundField() { DataField = "Timestamp", HeaderText = "Timestamp" };
            grid.Columns.Add(timestampField);
        }

        protected override void CreateChildControls() {
            base.CreateChildControls();

            PopulateDataset();

            if (data.IISState.Count == 0) {
                literal.Text = "No results collected.";
            } else {
                grid.DataSource = new DataView(data.IISState);
                grid.DataBind();
            }
        }
    }
}
